package com.ctgu.蓝桥杯真题.B组2014;

import java.util.Set;
import java.util.TreeSet;

/**
 * @author: Backdiy
 * @data: 2021/4/15 20:51
 * @desc:
 */
public class 扑克排序7 {
    static char[] cs = new char[]{'A', 'A', '2', '2', '3', '3', '4', '4'};
    static TreeSet<String> set = new TreeSet<>();

    public static void main(String[] args) {
        dfs(0, 7);
        System.out.println(set.first());
    }

    static void dfs(int n, int m) {
        if (n == m) {
            String s = new String(cs);
            if (check(s)) {
                set.add(s);
            }
            return;
        }
        for (int i = n; i <= m; i++) {
            swap(i,n);
            dfs(n+1,m);
            swap(i,n);
        }
    }

    static void swap(int i, int j) {
        char t = cs[i];
        cs[i] = cs[j];
        cs[j] = t;
    }

    static boolean check(String s) {
        if (s.lastIndexOf("A") - s.indexOf("A") == 2)
            if (s.lastIndexOf("2") - s.indexOf("2") == 3)
                if (s.lastIndexOf("3") - s.indexOf("3") == 4)
                    if (s.lastIndexOf("4") - s.indexOf("4") == 5)
                        return true;
        return false;
    }
}
